import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_core/x_core.dart';
import 'package:x_project/common/config.dart';
import 'package:x_project/components/x_scroll_view.dart';

/// Created by XieXin on 2020/10/21.
/// 页面类型枚举
/// 用于控制正常、暂无数据、网络异常页面显示
enum LoadState {
  normal, //正常
  noData, //暂无数据
  networkAnomaly, //网络异常
}

/// 图标大小
enum ViewStyle {
  small, //小
  medium, //小
  large, //大
}

/// Created by XieXin on 2020/10/20.
/// 加载状态部件
class LoadStateWidget extends StatelessWidget {
  final LoadState type;
  final ViewStyle style;
  final String? hintText;
  final VoidCallback? onPressed;
  final ScrollPhysics? physics;
  final bool shrinkWrap;

  const LoadStateWidget({
    Key? key,
    this.type = LoadState.noData,
    this.style = ViewStyle.large,
    this.hintText,
    this.onPressed,
    this.physics = const BouncingScrollPhysics(),
    this.shrinkWrap = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double size;
    switch (style) {
      case ViewStyle.small:
        size = 150.w;
        break;
      case ViewStyle.medium:
        size = 200.w;
        break;
      default:
        size = 400.w;
        break;
    }
    String hintTextTemp = hintText ?? '暂无数据 ~';
    String image = "";
    switch (type) {
      case LoadState.noData:
        image = 'ic_no_data';
        if (TextUtil.isEmpty(hintText)) {
          if (onPressed != null) {
            hintTextTemp = '暂无数据，点击刷新 ~';
          } else {
            hintTextTemp = '暂无数据 ~';
          }
        }
        break;
      case LoadState.networkAnomaly:
        image = 'ic_network_anomaly';
        if (TextUtil.isEmpty(hintText)) {
          if (onPressed != null) {
            hintTextTemp = '网络异常，点击刷新 ~';
          } else {
            hintTextTemp = '网络异常 ~';
          }
        }
        break;
      default:
        image = 'ic_no_data';
        if (TextUtil.isEmpty(hintText)) {
          if (onPressed != null) {
            hintTextTemp = '暂无数据，点击刷新 ~';
          } else {
            hintTextTemp = '暂无数据 ~';
          }
        }
        break;
    }
    return XScrollView(
      physics: physics,
      shrinkWrap: shrinkWrap,
      children: [
        InkWell(
          onTap: onPressed,
          child: Container(
            width: double.infinity,
            padding: EdgeInsets.symmetric(
              horizontal: 32.w,
              vertical: 50.w,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SizedBox(height: 100.h),
                Opacity(
                  opacity: Get.isDarkMode ? 0.5 : 1,
                  child: Container(
                    height: size,
                    width: size,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: ImageUtils.getAssetImageProvider(
                          image,
                          moduleName: Config.getModuleName(),
                        ),
                      ),
                    ),
                  ),
                ),
                SizedBox(height: 40.h),
                Text(
                  hintTextTemp,
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 30.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
